﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace RMS.Utils.Dicom.Commen
{
    static class Extensions
    {
        #region [ConvertExtensions]
        public static String GetString(this byte[] value)
        {
            return CharacterRepertoire.Encoding.GetString(value, 0, value.Length).Trim('\0');
        }
        public static UInt16 ToUInt16(this byte[] value)
        {
            value = CorrectByteOrdering(value);
            if (TransferSyntax.IsLittleEndian)
            {
                return (ushort)(value[0] | (value[1] << 8));
            }
            return (ushort)((value[0] << 8) | value[1]);
        }
        public static UInt32 ToUInt32(this byte[] value)
        {
            value = CorrectByteOrdering(value);
            if (TransferSyntax.IsLittleEndian)
            {
                return (uint)(((value[0] | (value[1] << 8)) | (value[2] << 0x10)) | (value[3] << 0x18));
            }
            return (uint)((((value[0] << 0x18) | (value[1] << 0x10)) | (value[2] << 8)) | value[3]);
        }
        public static DateTime ToDateTime(this byte[] value)
        {
            string tempString = CharacterRepertoire.Encoding.GetString(value, 0, value.Length);
            if (tempString.Length == 8)
                return DateTime.ParseExact(tempString, "yyyymmdd", System.Globalization.CultureInfo.InvariantCulture);
            else
                return DateTime.ParseExact(tempString, "yyyymmddhhmmss", System.Globalization.CultureInfo.InvariantCulture);
        }
        public static Object ToObject(this byte[] Value)
        {
            MemoryStream memStream = new MemoryStream();
            BinaryFormatter binForm = new BinaryFormatter();
            memStream.Write(Value, 0, Value.Length);
            memStream.Seek(0, SeekOrigin.Begin);
            Object obj = (Object)binForm.Deserialize(memStream);
            return obj;
        }
        #endregion

        #region [HelperMethod]
        private static byte[] CorrectByteOrdering(byte[] bytes)
        {
            if ((TransferSyntax.IsMachineLittleEndian && !TransferSyntax.IsLittleEndian) ||
                (!TransferSyntax.IsMachineLittleEndian && TransferSyntax.IsLittleEndian))
                return bytes.SwapBytes();
            else
                return bytes;
        }
        private static byte[] SwapBytes(this byte[] bytes)
        {
            byte[] buffer = new byte[bytes.Length];
            for (int i = 0; i < bytes.Length; i++)
                buffer[i] = bytes[bytes.Length - 1 - i];
            return buffer;
        }
        #endregion

        #region [ObjectExtensions]
        public static int ToInt32(this object value)
        {
            return Convert.ToInt32(value);
        }
        public static String ToHex(this object value)
        {
            return string.Format("{0:X4}", value);
        }

        public static byte[] ToByte(this object value)
        {
            if (value == null)
                return null;
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, value);
            return ms.ToArray();
        }
        #endregion




    }
}
